(function (G) {
    G.component({
        options: {
            multipleSlots: true
        },
        props: {
            name: {
                type: String,
                default: G.guid()
            },
            index: {
                type: Number
            },
            disabled: {
                type: Boolean,
                default: true
            },
            value: {
                type: String,
                default: {},
                observer: function (newValue, oldValue) {
                    this.filter(newValue);
                }
            },
            item: {
                type: Object,
                default: {},
            },
            attr: {
                type: Object,
                default: {}
            }
        },
        data: function () {
            return {
                longitude:'',
                latitude:'',
                address:''
            }
        },
        mounted: function () {
            let value = this.data.value || {},item = this.data.item
            if(typeof value == 'string') value = G.string(value).parse({})
            if(!value.latitude && !value.longitude && item.latitude && item.longitude){
                value.latitude = item.latitude
                value.longitude = item.longitude
                value.address = item.address || item.location
            }
            this.filter(value)
        },
        methods: {
            filter(value){
                if(typeof value == 'string') value = G.string(value).parse({})
                if(value && value.latitude && value.longitude){
                    this.setData({
                        latitude:value.latitude,
                        longitude:value.longitude,
                        address:value.address
                    })
                }
            },
            selectLocation(event) {
                if(this.data.disabled) {
                    wx.openLocation({
                        latitude:this.data.latitude,
                        longitude:this.data.longitude,
                        address:this.data.address
                    })
                }else{
                    wx.chooseLocation({
                        latitude:this.data.latitude,
                        longitude:this.data.longitude,
                        success:res=>{
                            this.setData({
                                latitude:res.latitude,
                                longitude:res.longitude,
                                address:res.address
                            })
                            this.triggerEvent('change', {
                                index: this.getData('index'),
                                id: this.getData('name'),
                                event,
                                valueObj:{
                                    latitude:this.data.latitude,
                                    longitude:this.data.longitude,
                                    address:this.data.address
                                },
                                value: JSON.stringify({
                                    latitude:this.data.latitude,
                                    longitude:this.data.longitude,
                                    address:this.data.address
                                })
                            })
                        }
                    })
                }
            }
        }
    })
})(wx.$)